package cn.rui.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 默认的退出成功处理器，如果设置了qaqavr.security.browser.signOutUrl，则跳到配置的地址上，
 * 如果没配置，则返回json格式的响应。
 *
 * @author yuyi
 * @version 1.0
 *
 */
@Slf4j
@Component
public class BrowserLogoutSuccessHandler implements LogoutSuccessHandler {

	/**
	 * (non-Javadoc)
	 *
	 * @see LogoutSuccessHandler#onLogoutSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, Authentication)
	 */
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {

		log.info("exit successfully");
		response.sendRedirect (request.getContextPath ()+"/login");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setStatus(HttpStatus.OK.value());
		response.getWriter().append("exit successfully");

	}

}
